define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'facilitatorexpand/index/facilitator_id/'+Config.facilitator_id + location.search,
                    add_url: 'facilitatorexpand/add/facilitator_id/'+Config.facilitator_id,
                    edit_url: 'facilitatorexpand/edit',
                    del_url: 'facilitatorexpand/del',
                    multi_url: 'facilitatorexpand/multi',
                    import_url: 'facilitatorexpand/import',
                    table: 'facilitator_expand',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'facilitator.title', title: __('Facilitator.title'), operate: 'LIKE'},
                        {field: 'facilitator.name', title: __('联系人'), operate: 'LIKE'},
                        {field: 'facilitator.mobile', title: __('手机号码'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
                        // {field: 'facilitator_id', title: __('Facilitator_id')},
                        {field: 'admin_id', title: __('管理员id'),visible: false},
                        {field: 'plantime', title: __('Plantime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'expandtime', title: __('Expandtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'plan_content', title: __('Plan_content'), operate: 'LIKE'},
                        {field: 'way', title: __('Way'), operate: 'LIKE'},
                        // {field: 'stage', title: __('Stage'), operate: 'LIKE'},
                        {field: 'content', title: __('Content'), operate: 'LIKE'},
                        {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'facilitator.introducer', title: __('采单人'), operate: 'LIKE'},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, buttons: [
                                {name: 'promote', text: '跟进', title: '跟进',extend: 'data-area=["100%","100%"]', classname: 'btn btn-xs btn-primary btn-dialog',
                                    url:function(row){
                                        return 'facilitatorexpand/promote/ids/'+row.id;
                                    },
                                    hidden:function(row){
                                        if(row.status == 1 || row.status == 3){
                                            return true
                                        }
                                        return false
                                    },
                                },
                                {name: 'cancel', text: '取消', title: '取消',extend: 'data-area=["60%","60%"]', classname: 'btn btn-xs btn-primary btn-dialog',
                                    url:function(row){
                                        return 'facilitatorexpand/cancel/ids/'+row.id;
                                    },
                                    hidden:function(row){
                                        if(row.status == 1 || row.status == 3){
                                            return true
                                        }
                                        return false
                                    },
                                },
                                {
                                    name: 'edit', // 自定义编辑按钮
                                    classname: 'btn btn-xs btn-success btn-editone',
                                    icon: 'fa fa-pencil',
                                    url:function(row){
                                        if(row.status == 0 || row.status == 2){
                                            return 'facilitatorexpand/planedit/ids/'+row.id
                                        }
                                        return 'facilitatorexpand/promoteedit/ids/'+row.id;
                                    },
                                    hidden:function(row){
                                        if(row.status == 3){
                                            return true
                                        }
                                        return false
                                    },
                                },
                            ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        facilitator: function () {
            // 初始化表格参数配置
            Table.api.init();
            this.table.first();
            this.table.second();
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                table1.bootstrapTable({
                    url: 'facilitatorexpand/facilitator/status/0/facilitator_id/'+Config.facilitator_id + location.search,
                    extend: {
                        index_url: 'facilitatorexpand/index/facilitator_id/'+Config.facilitator_id + location.search,
                        add_url: 'facilitatorexpand/add/facilitator_id/'+Config.facilitator_id,
                        edit_url: 'facilitatorexpand/planedit',
                        del_url: 'facilitatorexpand/del',
                        multi_url: 'facilitator/multi',
                        import_url: 'facilitator/import',
                        table: 'table1',
                    },
                    toolbar: '#toolbar1',
                    sortName: 'id',
                    search: false,
                    showToggle: false,
                    showColumns: false,
                    showExport: false,
                    commonSearch: false,
                    columns: [
                        [
                            {field: 'facilitator.title', title: __('Facilitator.title'), operate: 'LIKE'},
                            {field: 'facilitator.name', title: __('联系人'), operate: 'LIKE'},
                            {field: 'facilitator.mobile', title: __('手机号码'), operate: 'LIKE'},
                            {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                            {field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
                            // {field: 'facilitator_id', title: __('Facilitator_id')},
                            // {field: 'admin_id', title: __('Admin_id')},
                            {field: 'plantime', title: __('Plantime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            // {field: 'expandtime', title: __('Expandtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            // {field: 'plan_content', title: __('Plan_content'), operate: 'LIKE'},
                            // {field: 'way', title: __('Way'), operate: 'LIKE'},
                            // {field: 'stage', title: __('Stage'), operate: 'LIKE'},
                            // {field: 'content', title: __('Content'), operate: 'LIKE'},
                            // {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                            // {field: 'facilitator.introducer', title: __('采单人'), operate: 'LIKE'},
                            {field: 'operate', title: __('Operate'), table: table1, buttons: [
                                    {name: 'promote', text: '跟进', title: '跟进',extend: 'data-area=["80%","80%"]', classname: 'btn btn-xs btn-primary btn-dialog',
                                        url:function(row){
                                            return 'facilitatorexpand/promote/ids/'+row.id;
                                        },
                                        hidden:function(row){
                                            if(row.status == 1){
                                                return true
                                            }
                                            return false
                                        },
                                    },
                                    {name: 'cancel', text: '取消', title: '取消',extend: 'data-area=["60%","60%"]', classname: 'btn btn-xs btn-primary btn-dialog',
                                        url:function(row){
                                            return 'facilitatorexpand/cancel/ids/'+row.id;
                                        },
                                        hidden:function(row){
                                            if(row.status == 1){
                                                return true
                                            }
                                            return false
                                        },
                                    },
                                    // {
                                    //     name: 'edit', // 自定义编辑按钮
                                    //     classname: 'btn btn-xs btn-success btn-editone',
                                    //     icon: 'fa fa-pencil',
                                    //     url: 'facilitatorexpand/planedit',
                                    // },
                                ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'facilitatorexpand/facilitator/status/1/facilitator_id/'+Config.facilitator_id + location.search,
                    extend: {
                        index_url: 'facilitatorexpand/index/facilitator_id/'+Config.facilitator_id + location.search,
                        add_url: 'facilitatorexpand/add/facilitator_id/'+Config.facilitator_id,
                        edit_url: 'facilitatorexpand/promoteedit',
                        del_url: 'facilitatorexpand/del',
                        multi_url: 'facilitator/multi',
                        import_url: 'facilitator/import',
                        table: 'table2',
                    },
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    showToggle: false,
                    showColumns: false,
                    showExport: false,
                    commonSearch: false,
                    columns: [
                        [
                            {field: 'facilitator.title', title: __('Facilitator.title'), operate: 'LIKE'},
                            {field: 'facilitator.name', title: __('联系人'), operate: 'LIKE'},
                            {field: 'facilitator.mobile', title: __('手机号码'), operate: 'LIKE'},
                            {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                            {field: 'admin.nickname', title: __('Admin.nickname'), operate: 'LIKE'},
                            // {field: 'facilitator_id', title: __('Facilitator_id')},
                            // {field: 'admin_id', title: __('Admin_id')},
                            // {field: 'plantime', title: __('Plantime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            {field: 'expandtime', title: __('Expandtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                            // {field: 'plan_content', title: __('Plan_content'), operate: 'LIKE'},
                            {field: 'type.name', title: __('Way'), operate: 'LIKE'},
                            // {field: 'stage', title: __('Stage'), operate: 'LIKE'},
                            {field: 'content', title: __('Content'), operate: 'LIKE'},
                            {field: 'images', title: __('Images'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                            {field: 'cancel_remark', title: __('取消原因'), operate: 'LIKE'},
                            // {field: 'facilitator.introducer', title: __('采单人'), operate: 'LIKE'},
                            {field: 'operate', title: __('Operate'), table: table2, buttons: [
                                    // {
                                    //     name: 'edit', // 自定义编辑按钮
                                    //     classname: 'btn btn-xs btn-success btn-editone',
                                    //     icon: 'fa fa-pencil',
                                    //     url: 'facilitatorexpand/promoteedit',
                                    // },
                                ], events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            }
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'facilitatorexpand/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'facilitatorexpand/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'facilitatorexpand/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        import_expand: function () {
            Controller.api.bindevent();
        },
        promote: function () {
            Controller.api.bindevent();
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        promoteedit: function () {
            Controller.api.bindevent();
        },
        cancel: function () {
            Controller.api.bindevent();
        },
        planedit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
